const app = getApp();
var url = app.getURL();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:'',
    list:{},
    commentsList:[],
    good:false,
    count:0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // console.log(options)
    // 获取上一页面传的参数
    var that = this
    let id = options.id;
    this.setData({
      id: id
    })
    //当页面加载时请求数据
    this.getDetail(id)
  },
  getDetail(id){
    let that = this
    wx.request({
      url:url+'/comm/app/comm/lostFound/queryDetail/'+id,
      method: "GET",
      header: {
        'content-type': 'application/json',
        token:wx.getStorageSync('cookieToken')
      },
      success(res) {
        console.log(res.data.data)
        that.setData({
          "list": res.data.data,
          "good": res.data.data.isLiked,
          "count": res.data.data.likeTotal
        })
      }
    })
  },
  // 发表评论
  Comment(e){
    console.log('form发生了submit事件，携带的数据为：', e.detail.value)
    let that = this
    let id = wx.getStorageSync('message').id;
    wx.request({
      url:url+'/comm/app/comm/lostFound/auth/lostFoundComment',
      method: "POST",
      header: {
        'content-type': 'application/json',
        token:wx.getStorageSync('cookieToken')
      },
      data:{
        "commentContent": e.detail.value.comment,
        "contentId": that.data.id,
        "custId": id
      },
      success(res) {
        console.log(res)
        if(res.data.code==200){
          wx.showToast({
            title: '评论成功',
            duration: 600,
            icon: 'success',
          })
          that.getDetail(that.data.id)
        }else{
          wx.showToast({
            title: '评论失败',
            duration: 600,
            icon: 'error',
          })
        }
      }
    })
  },

  // 点赞
good(){
  let that = this
  let id = wx.getStorageSync('message').id;
  let count = this.data.count;
  wx.request({
    url:url+'/comm/app/comm/lostFound/auth/lostFoundLike',
    method: "POST",
    header: {
      'content-type': 'application/json',
      token:wx.getStorageSync('cookieToken')
    },
    data:{
      "commType": 1300,
      "likeContentId": that.data.id,
      "likedAuthorId": that.data.list.authorId,
      "postLikeId": id
    },
    success(res) {
      console.log(res)
      if(res.data.code==200){
        wx.showToast({
          title: '点赞成功',
          duration: 600,
          icon: 'success',
        })
        that.setData({
          good: true,
          count: count + 1
        })
      }else{
        wx.showToast({
          title: '点赞失败',
          duration: 600,
          icon: 'error',
        })
      }
    }
  })
},
  // 取消点赞
  unGood(){
    let that = this
    let id = wx.getStorageSync('message').id;
    let count = this.data.count;
    wx.request({
      url:url+'/comm/app/comm/lostFound/auth/lostFoundUnLike',
      method: "POST",
      header: {
        'content-type': 'application/json',
        token:wx.getStorageSync('cookieToken')
      },
      data:{
        "commType": 1300,
        "likeContentId": that.data.id,
        "likedAuthorId": that.data.list.authorId,
        "postLikeId": id
      },
      success(res) {
        if(res.data.code==200){
          wx.showToast({
            title: '取消点赞成功',
            duration: 600,
            icon: 'success',
          })
          that.setData({
            good: false,
            count: count - 1
          })
        }else{
          wx.showToast({
            title: '取消点赞失败',
            duration: 600,
            icon: 'error',
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})